﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace ReveiwWord
{
    public partial class Form1 : Form
    {
        private HandleExl handleExl;
        private HandleXML handleXML;
        private string[] word={null,null,null};

        private static int indexW = 0;

        public Form1()
        {
            InitializeComponent();
            MyInitializeComponent();
        }

        private void MyInitializeComponent()
        {
            handleExl = new HandleExl(@"Word.xls");
            handleXML = new HandleXML();
            if (File.Exists(@"Word.xls"))
            {
                buttonStart.Enabled = true;
                buttonResetWord.Enabled = true;
            }
        }

        private void UpdateTextBox(int indexWord)
        {
            word = handleExl.GetRowFormExl(indexWord);
            alphaBlendTextBoxWord.Text = word[0];
            alphaBlendTextBoxTranslate.Text = word[1];
            alphaBlendTextBoxPhonetic.Text = word[2];

        }




        private void buttonNewTxtFile_Click(object sender, EventArgs e)
        {
            String path=null;
            System.Windows.Forms.DialogResult result = openFileDialog1.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                path = openFileDialog1.FileName;
            }
            if (path != null)
            {
                handleExl.CreatExlWord(path);
            }

            buttonPreviousWord.Enabled = true;
            buttonNextWord.Enabled = true;
           
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            int.TryParse(handleXML.CurrentID(), out indexW);
            UpdateTextBox(indexW);
            alphaBlendTextBoxLog.Text="上次学习到第"+handleXML.CurrentID()+"单词"+"\n"+" "+"\n"+word[0];
           
            buttonPreviousWord.Enabled = true;
            buttonNextWord.Enabled = true;
        }



        private void alphaBlendTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (word[0] != null)
                {
                    alphaBlendTextBox1.Clear();
                    UpdateTextBox(++indexW);
                }
                
                else
                {
                    alphaBlendTextBoxLog.Text = "你已经完成所有词汇学习！是否重新开始";
                }
            }
            else
            {
                if (word[0] == null)
                {
                    e.Handled = false;
   //                 
                    alphaBlendTextBoxLog.Text = "你没有选择新的单词簿，请选择继续上次";
                   
                }

            }
            
        }

        private void alphaBlendTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (word[0] != null)
            {
                char[] ch = word[0].ToCharArray();
                int iLength = alphaBlendTextBox1.Text.Length;
                string sText = alphaBlendTextBox1.Text;
                if (iLength > word[0].Length)
                    alphaBlendTextBox1.ForeColor = Color.Red;
                else if (!sText.Equals(word[0].Substring(0, iLength)))
                    alphaBlendTextBox1.ForeColor = Color.Red;
                else
                    alphaBlendTextBox1.ForeColor = Color.Black;
            }
            
               
        }

        private void buttonNextWord_Click(object sender, EventArgs e)
        {
            UpdateTextBox(++indexW);
        }

        private void buttonPrevious_Click(object sender, EventArgs e)
        {
            if(indexW!=0)
            UpdateTextBox(--indexW);
        }




        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            handleXML.WriteXML(Convert.ToString(indexW), DateTime.Now.ToString(), word[0]);
        }

        private void buttonResetWord_Click(object sender, EventArgs e)
        {
            indexW = 0;
            UpdateTextBox(indexW);
            buttonPreviousWord.Enabled = true;
            buttonNextWord.Enabled = true;
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


    }
}
